// xc_lshop/shop/user/barter/money/com/order.js
var barter_order = require('../../../../model/barter_order')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
     unit:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    searchmodel:{
      isactive:-1,
      examstatus:2
    },
    checked:{},
    amount:0,
    num:0
  },
  ready(){
   
  },
  /**
   * 组件的方法列表
   */
  methods: {
    makeamount(){
        var modellist=this.data.modellist;
        var checked=this.data.checked;
        var amount=0;
        var num=0;
        for(var i in modellist){
            if(checked[modellist[i]['id']]){
              amount +=modellist[i]['price'] * 1 * modellist[i]['num'];
              num +=1;
            }
        }
        this.setData({
          amount:amount,
          num:num
        })
    },
    onClick(e){
      var id=e.currentTarget.dataset.id +'';
      var upte={}
      upte['checked.'+id]=!this.data.checked[id]
      this.setData(upte)
      this.makeamount()
    },
     init(checked){
      var that = this
      that.getpagelist(that, 1)
      if(checked){
        this.setData({
          checked:checked
        })
        this.makeamount()
      }
      this.setData({
        show:true
      })
     },
     initmoney(){
      var that = this
      that.getlist()
      
    },
    getlist(){
      var that = this;
      barter_order.getorderlist(this.data.searchmodel).then(function (res) {
        if (res['status'] == 1) {
          var obj = res['obj']
          // for(var i in obj['modellist']){
          //   obj['modellist'][i]['nums']=1
          //   obj['modellist'][i]['xxx']=2

          // }
          var update = {list:obj['modellist']}
          console.log("ddddddddddddddd")
          console.log(update)

          that.setData(update)
          that.check(obj['modellist'])
        }
      })
    },
    check(list){
      console.log("list")
      console.log(list)
      var amount=0;
      var num=0;
      var upte={}
      for(var i in list){
        upte['checked.'+list[i]['id']]=true
        amount +=list[i]['price'] * 1 * list[i]['num'];
        num +=1;
      }

      upte['amount']=amount;
      upte['num']=num;
      this.setData(upte)
      this.triggerEvent("onchoose",{checked:this.data.checked,amount:this.data.amount})
    },
    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        strwhere = this.data.searchmodel;
        // if(!strwhere)
        // {
        //   strwhere={}
        // }
        // strwhere["status"] = this.data.activetab
        that.setData({
          modellist: [],
          isempty: false
        });
      }
      barter_order.offset(that, rest, strwhere).then(function (result) {
        var updatemodel = {};
        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
          if (result["obj"]["modellist"].length < 1 && that.data.modellist.length < 1) {
            updatemodel["isempty"] = true
          } else if (rest == 1) {
            updatemodel["modellist"] = result["obj"]["modellist"];
          } else {
            if (result["obj"]["modellist"].length > 0) {
              updatemodel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
            }
          }
        }
        // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
        wx.stopPullDownRefresh();
        that.setData(updatemodel);
      }, function () {
  
      });
    },
    onreachbottom(){
      var that=this;
      this.getpagelist(that,-1)
    },
    onClickBtn(){
      if(this.data.num==0){
         wx.showToast({
           icon:"none",
           title: '至少选择一个订单',
         })
      }else{
        this.triggerEvent("onchoose",{checked:this.data.checked,amount:this.data.amount})
        this.onClose()
      }
    },
    onClose(){
      this.setData({
        show:false
      })
    }
  }
})
